package com.coding.controller;

import com.coding.base.pojo.BaseModel;
import com.coding.service.PoempkService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;


@Controller
@ResponseBody
@RequestMapping("MyServer")
public class MyServer {
    //定义保存所有Socket的Arraylist
    public static ArrayList<Socket> socketArrayList = new ArrayList<Socket>();
    private boolean flag = true;
    @Autowired
    PoempkService poempkService;

    @ApiOperation(value = "server测试")
    @RequestMapping(value = "/startserver", method = RequestMethod.POST)
    @ResponseBody
    public BaseModel startserver() {
        BaseModel baseModel = new BaseModel();
        try {
            ServerSocket serverSocket = new ServerSocket(39000);
            serverSocket.setSoTimeout(20000);
            while (flag) {
                Socket s = serverSocket.accept();
                System.out.println("当前地址为:" + s.getInetAddress());
//                s.setSoTimeout(100);
                socketArrayList.add(s);
                new Thread(new ServerThread(s)).start();
                if (socketArrayList.size() >= 2) {
                    flag = false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseModel.setData(socketArrayList.size());
        return baseModel;
    }

    @ApiOperation(value = "server连接")
    @RequestMapping(value = "/teststartserver", method = RequestMethod.POST)
    @ResponseBody
    public BaseModel teststartserver(@ApiParam(value = "user_id", required = true) @RequestParam("user_id") int user_id) {
        BaseModel baseModel = new BaseModel();
        try {
            poempkService.start(user_id,baseModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseModel;
    }

    @ApiOperation(value = "server测试")
    @RequestMapping(value = "/stopserver", method = RequestMethod.POST)
    @ResponseBody
    public BaseModel stopserver() {
        BaseModel baseModel = new BaseModel();
        if (socketArrayList.size() > 0) {
            for (Socket socket : socketArrayList
            ) {
                try {
                    System.out.println(socket.getInetAddress() + "关闭");
                    socket.close();

                } catch (IOException e) {
                    System.out.println(socket.getInetAddress() + "关闭失败");
                    e.printStackTrace();
                }
            }
        }
        socketArrayList.clear();
        baseModel.setData(socketArrayList.size());
        baseModel.setResultCode(1);
        baseModel.setMessage("已关闭所有进程");

//        try {
//            ServerSocket serverSocket=new ServerSocket(30000);
//            while (true){
//                Socket s=serverSocket.accept();
//                socketArrayList.add(s);
//                new Thread(new ServerThread(s)).start();
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        return baseModel;
    }

    @ApiOperation(value = "server测试")
    @RequestMapping(value = "/getserver_status", method = RequestMethod.POST)
    @ResponseBody
    public BaseModel getserver_status() {
        BaseModel baseModel = new BaseModel();
        if (socketArrayList.size() > 0) {
            for (Socket socket : socketArrayList
            ) {
                System.out.println(socket.getInetAddress() + ":" + socket.getChannel());
            }
        }

        baseModel.setData(socketArrayList.size());
        baseModel.setResultCode(1);
        baseModel.setMessage("目前线程个数");
        return baseModel;
    }

    @ApiOperation(value = "server测试")
    @RequestMapping(value = "/getimplserver_status", method = RequestMethod.POST)
    @ResponseBody
    public BaseModel getimpl_status() {
        BaseModel baseModel = new BaseModel();
        try {
            poempkService.getstatus(baseModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseModel;
    }
}
